home *** CD-ROM | disk | FTP | other *** search
- Path: netnews.fast.net!news
- From: 73541.1332@compuserve.com
- Newsgroups: comp.lang.c++
- Subject: Re: C++ problem with constructor.
- Date: 23 Mar 1996 02:41:20 GMT
- Organization: via FASTNET(tm) PA/NJ/DE Internet Services
- Message-ID: <4ivocg$e9d@nn2.fast.net>
- References: <DoGGGp.Muy@latcs1.lat.oz.au>
- Reply-To: 73541.1332@compuserve.com
- NNTP-Posting-Host: power.gpu.com
- X-Newsreader: IBM NewsReader/2 v1.2
-
- Gregary, try new'g some storage for street and city before strcpy
-
-
-
-
- In <DoGGGp.Muy@latcs1.lat.oz.au>, boylesgj@lion.cs.latrobe.edu.au (Gregary J Boyles) writes:
- >See astericks
- >
- >// main.cpp
- >
- >#include <iostream.h>
- >#include "defines.h"
- >#include "address.h"
- >
- >int main()
- >{
- > Address Address1(56,"Derby Drive","Epping",3165);
- > Address Address2(22,"Claremont Street","Fawkner",3060);
- >
- > /********************************************************
- > After the above two calls Address1 and Address2 contain
- > all the appropriate data however after the next call all
- > 3 contain 0 or null strings. WHY?
- > ********************************************************/
- >
- > Address Address3(Address1);
- > return(0);
- >}
- >
- >
- >
- >// address.cpp
- >
- >#include "address.h"
- >#include <string.h>
- >
- >// Constructors
- >Address::Address(int ANumber,const char *AStreet,const char *ACity,int AZip)
- >{
- > Number=ANumber;
- > strcpy(Street,AStreet);
- > strcpy(City,ACity);
- > Zip=AZip;
- >}
- >
- >Address::Address()
- >{
- > Number=0;
- > strcpy(Street,"");
- > strcpy(City,"");
- > Zip=0;
- >}
- >
- >// Copy constructor
- >Address::Address(Address& AnAddress)
- >{
- > Number=AnAddress.Number;
- > strcpy(Street,AnAddress.Street);
- > strcpy(City,AnAddress.City);
- > Zip=AnAddress.Zip;
- >}
- >
- >// Deconstructor
- >Address::~Address()
- >{
- > Number=0;
- > strcpy(Street,"");
- > strcpy(City,"");
- > Zip=0;
- >}
- >
- >
- >// address.h
- >
- >#ifndef __ADDRESS_H
- >#define __ADDRESS_H
- >#define STR_STREET " street"
- >
- >#include "defines.h"
- >
- >class Address
- >{
- > private : int Number;
- > char *Street;
- > char *City;
- > int Zip;
- >
- > public : // Constructors
- > Address(int ANumber,const char *AStreet,const char *ACity,int Zip);
- > Address();
- > // Copy constructor
- > Address(Address& AnAddress);
- > // Deconstructor
- > ~Address();
- >};
- >
- >#endif
-
-